package com.yuma.assistant;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        // 主线程根本不知道自线程什么执行完
        // 1.构造中给的是要等的线程数
        CountDownLatch countDownLatch = new CountDownLatch(6);

        for(var i = 1 ;i <= 6; i++){

            new Thread(() -> {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "走出班级");
                // 2.执行一个就自减
                countDownLatch.countDown();
            }, "学生" + String.valueOf(i)).start();
        }
        // 3.阻塞等待
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + ":班长锁门");
    }
}
